package com.software.car.service;

import com.github.pagehelper.PageHelper;
import com.software.car.api.UserService;
import com.software.car.common.entity_change.UserChangeMapper;
import com.software.car.common.enums.ResultEnum;
import com.software.car.common.exception.MyException;
import com.software.car.common.utils.CommonPage;
import com.software.car.common.utils.SnowflakeIdGenerator;
import com.software.car.entity.dto.*;
import com.software.car.entity.vo.UserDetailVo;
import com.software.car.entity.po.UserPo;
import com.software.car.mapper.UserMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 魔法はまだ解けない
 * @Date 2021/1/23
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMessageMapper userMessageMapper;

    @Override
    public List<UserDetailVo> getAllUserResponse(int state) {
        List<UserPo> allUserDetailVo = userMessageMapper.getAllUserResponse(state,null,null);
        List<UserDetailVo> collect = allUserDetailVo.stream().
                map(e -> UserChangeMapper.INSTANCE.UserPoToUserDetailVo(e)).
                collect(Collectors.toList());
        return collect;
    }

    @Override
    public CommonPage getUsersByPage(Integer page, Integer pageSize, Integer state,String query,Integer role) {
        PageHelper.startPage(page, pageSize);
        System.out.println("".equals(query));
        System.out.println(state);
        System.out.println(role);
        List<UserPo> allUserDetailVo = userMessageMapper.getAllUserResponse(state,query,role);

        CommonPage wrap = CommonPage.wrap(allUserDetailVo);
        List<UserDetailVo> collect = wrap.getList().stream().
                map(e -> UserChangeMapper.INSTANCE.UserPoToUserDetailVo((UserPo) e)).
                collect(Collectors.toList());
        wrap.setList(collect);
        return wrap;
    }

    @Override
    public UserDetailVo getUser(String id) {
        UserPo user = userMessageMapper.getUser(id);
        return UserChangeMapper.INSTANCE.UserPoToUserDetailVo(user);
    }

    @Override
    public UserPo getUserPo(String id) {
        UserPo user = userMessageMapper.getUserPo(id);
        return user;
    }

    @Override
    public UserDetailVo getUser(WebLoginDto webLoginDto) {
        UserDetailVo user = userMessageMapper.getUserByAccountPassword(webLoginDto.getAccount(), webLoginDto.getPassword());
        if (user == null) {
            throw new MyException(ResultEnum.LOGIN_EXCEPTION);
        }
        return user;
    }

    @Override
    public UserDetailVo getUser(WeChatLoginDto weChatLoginDto) {

        return null;
    }

    @Override
    public int insertUser(UserPo userPo) {
        return userMessageMapper.insertUser(userPo);
    }

    @Override
    public int insertUser(UserInsertDto userInsertDto) {
        UserPo userPo = UserPo.builder()
                .id(SnowflakeIdGenerator.getId())
                .name(userInsertDto.getName())
                .account(userInsertDto.getAccount())
                .password(userInsertDto.getPassword())
                .role(userInsertDto.getRole())
                .img("http://116.62.247.153:8091/photoload/ea167dc158e445bbb3a31073880080d6")
                .state(1)
                .sex(userInsertDto.getSex())
                .updateTime(new Date())
                .createTime(new Date())
                .build();
        if (userInsertDto.getEmail() != null) {
            userPo.setEmail(userInsertDto.getEmail());
        }
        if (userInsertDto.getPhone() != null) {
            userPo.setPhone(userInsertDto.getPhone());
        }
        int i = insertUser(userPo);
        return i;
    }

    @Override
    public UserDetailVo getUserByAccount(String account) {
        UserDetailVo userByAccountPassword = userMessageMapper.getUserByAccountPassword(account, null);
        return userByAccountPassword;
    }

    @Override
    public UserDetailVo userUpdateUser(UserPersonalUpdateDto updateUser, UserPo baseUser) {
        //对象转化
        UserPo userPo = UserChangeMapper.INSTANCE.userPersonalChangeDtoToPo(updateUser, baseUser.getId());
        //修改用户信息
        userMessageMapper.updateUser(userPo);
        //获取新信息
        UserPo user = userMessageMapper.getUser(baseUser.getId());
        return UserChangeMapper.INSTANCE.UserPoToUserPersonalVo(user);
    }

    @Override
    public UserDetailVo administerUpdateUser(UserAdministerUpdateDto updateUser) {
        validateUser(updateUser);
        //对象转化
        UserPo userPo1 = UserChangeMapper.INSTANCE.UserAdministerUpdateDtoToPo(updateUser);
        //修改用户信息
        System.out.println(userPo1);
        userMessageMapper.updateUser(userPo1);
        // 获取用户信息
        UserPo userPo = getUserPo(updateUser.getId());
        return UserChangeMapper.INSTANCE.UserPoToUserDetailVo(userPo);
    }

    @Override
    public void validateUser(UserAdministerUpdateDto updateUser) {
        UserDetailVo userByAccount = getUser(updateUser.getId());
        UserDetailVo userByAccount1 = getUserByAccount(updateUser.getAccount());
        //用户是否存在
        if (userByAccount == null) {
            throw new MyException(ResultEnum.USER_NOT_EXIT);
        }
        //是否是管理员
        if(userByAccount.getRole()==6){
            throw new MyException(ResultEnum.PASSWORD_CHANGE_ERROR);
        }
        //账号是否存在
        if (userByAccount1!=null){
            throw new MyException(ResultEnum.ACCOUNT_HAS_EXIT);
        }
    }

}
